package com.capgemini.rdlgae.service

import java.text.SimpleDateFormat;

import sun.util.logging.resources.logging;

import com.capgemini.rdlgae.domain.Commande 
import com.capgemini.rdlgae.domain.Dessert 
import com.capgemini.rdlgae.domain.Entree 
import com.capgemini.rdlgae.domain.Plat 
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

class CommandeService {

	boolean transactional = false
	
	def BigDecimal calculerTotal(Commande commande){	
		return calculerTotal(commande?.entree, commande?.plat, commande?.dessert)
	}
	
	def boolean isRead(Date date){
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
		String d = sdf.format(date);
		log.info "Vérification de la lecture de la commande du ${d}"
		
		try{
			def http = new HTTPBuilder("http://rdlgae.appspot.com")
			
			http.get( path : '/checkorder',
			contentType : TEXT,
			query : [date:d] ) { resp, reader ->
				println "response status: ${resp.statusLine}"
				return true
			}
			
		}
		catch ( Exception ex ) {
			return false
		}
		
		
	}
	
	def BigDecimal calculerTotal(Entree entree, Plat plat, Dessert dessert) {
		
		def total = 0.0
		if(plat?.menu){
			total = 6.4
			if(entree != null && dessert != null)
			total = 7.2
		}
		else {
			total = total.add(plat?.prix)
			if(entree != null)
			total = total.add(entree.prix)
			if(dessert != null)
			total = total.add(dessert.prix)
		}
		
		return total
	}
}
